package com.ljx.orm.util;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;

/**
 * @author LJX
 * @Description: TODO
 * @Date: 2022/10/28 14:10
 * @Version 1.0
 */
public class DBUtil {
	private static String driverName="";
	private static String url="";
	private static String username="";
	private static String password="";


	static {
		try {
			Properties properties = new Properties();
			InputStream inputStream = ClassLoader.getSystemResourceAsStream("db.properties");
			properties.load(inputStream);
			driverName = properties.getProperty("jdbc.driverName");
			url = properties.getProperty("jdbc.url");
			username = properties.getProperty("jdbc.username");
			password = properties.getProperty("jdbc.password");

		}catch (Exception e){
			throw new RuntimeException("无法读取db.properties文件异常");
		}
	}

	public static Connection getConnection() throws Exception{
		// 加载驱动
		Class.forName(driverName);
		// 获取连接对象
		Connection connection = DriverManager.getConnection(url, username, password);
		return connection;
	}

	// 关闭资源
	public static void closeAll(Connection conn, PreparedStatement ps, ResultSet rs){
		try{
			if(rs!=null){
				rs.close();
			}
			if(ps!=null){
				ps.close();
			}
			if(conn!=null){
				conn.close();
			}
		}catch (Exception e){

		}
	}


}
